﻿@model IEnumerable<Wicresoft.UnifyShow.Business.BusinessSystemModel>
@{
    ViewBag.Title = "维护业务系统";
    var types = new DeviceAndTypes().Types;
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    Dictionary<decimal, string> idAndSiteNames = (Dictionary<decimal, string>)ViewData["SiteIDAndName"];
    List<decimal> listSiteIDs = Model.Select(o => o.SiteID).Distinct().OrderBy(o => o).ToList();
}
@*<div style="display: none">
    <div id="bsTreeControl">
        <a title="close" href="#close">
            <img src="../../Content/images/minus.gif" />关闭所有</a> <a title="" href="#plus">
                <img src="../../Content/images/plus.gif" />展开所有</a> <a title="toggle" href="#toggle">
                    开关交替</a>
    </div>
</div>*@
<div class="right_title2">
    <a href="/Navigate/Index">首页</a> ><span>维护业务系统</span></div>
<div class="index_lr">
    <input type="hidden" id="systemID" />
    <ul>
        <li class="index_lr4_L">
            <div id="tree">
                @Html.Action("Tree")
            </div>
        </li>
        <li class="index_lr4_R">
            <ul>
                <li class="index_lr4_R">
                    <div class="index_lrA_title ui-icons">
                        <h3>
                            业务系统：</h3>
                    </div>
                    <div id="content">
                    </div>
                </li>
                <li class="index_lr4_R">
                    <div class="index_lrA_title ui-icons">
                        <h3>
                            此业务系统的设备列表：</h3>
                    </div>
                    <div class="index_pt_bg">
                        <table>
                            <tbody>
                                <tr>
                                    <td width="220">
                                        <label>
                                            特性值：</label>
                                        <input type="text" id="searchIdentify" name="searchIdentify" />
                                    </td>
                                    <td width="220">
                                        <label>
                                            分类：</label>
                                        <select name="searchType" id="searchType">
                                            <option value=""></option>
                                            @foreach (string item in types)
                                            {
                                                <option value="@item">@item</option>
                                            }
                                        </select>
                                    </td>
                                    <td width="220">
                                        <input type="button" id="searchDevice" value="查询" />
                                        <input type="button" id="searchClearDevice" value="重置" />
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <div id="devices" data-url="">
                        </div>
                    </div>
                </li>
                <li class="index_lr4_R">
                    <div class="index_lrA_title ui-icons">
                        <h3>
                            未相关业务系统的设备列表：</h3>
                    </div>
                    <div class="index_pt_bg">
                        <div id="devicesNOBSID" data-url="">
                        </div>
                    </div>
                </li>
            </ul>
</div>
<div id="deviceEditDialog" title="编辑设备">
</div>
<div id="deviceAddDialog" title="添加设备">
</div>
<div class="clear">
</div>
@section LeftMenu{
    <li><a href="@Url.Action("CreateUser", "AdminPermission")">
        创建用户</a></li>
    <li><a href="@Url.Action("Index", "AdminPermission")">配置用户</a></li>
    <li><a href="@Url.Action("Add", "AdminBS")">添加业务系统</a></li>
    <li class="VNavLeaf ui-icons">维护业务系统</li>
    <li><a href="@Url.Action("Admin", "AdminDocument")">常用资源管理</a></li>
    <li><a href="@Url.Action("Index", "AdminOther")">其他管理</a></li>
    @*Javascript向导 参考  当UserSites.count>3时，一次取出，否则分别取出。可以仿造权限 来选择 业务系统范围    
    device 增删改查  列表  通过dialog 进行增加和 编辑*@
}
@section ScriptAndCss{
    <link href="../../Content/jquery.treeview.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/css/bsadmin.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.treeview.js" type="text/javascript"></script>
   @* <script src="../../Scripts/jquery-1.4.4-vsdoc.js" type="text/javascript"></script>*@
    <script type="text/javascript">
        function  initTheEditor(){
            $("#bsSave","#content").click(function()
            {
              $.post("@Url.Action("Edit", "AdminBS")",
                        $("#bsEditForm").serialize()+"&Name="+$("#Alias","#bsEditForm").val(),//+"&SiteId="+$("#bsSiteID","#bsEditForm").val(),
                        function () {
                           alert("更新成功");
                        });
            });
            $("#bsPublish","#content").click(function()
            {
              $.post("@Url.Action("Publish", "AdminBS")",
                        "id="+$("#bsID","#bsEditForm").val(),
                        function () {
                           alert("发布成功");
                        });
            });
            $("#bsDelete","#content").click(function()
            {
              var deviceCount=$("tbody","#gridDeviceTable").children().length;
              if(deviceCount>0)
              {
              alert("此业务系统有关联的设备，请删除设备后，再删除此业务系统。");
              return false;
              }
              var isContinue=confirm("你确定要删除此业务系统？");
              if(isContinue)
              {
              $.post("@Url.Action("Delete", "AdminBS")",
                        "id="+$("#bsID","#bsEditForm").val(),
                        function () {
                           alert("删除成功");
                           var url="@Url.Action("Tree", "AdminBSMaintain")";
                           $("#tree").load(url,function(){
                           treeView();
                           });
                        });
                        }
            });
            }

           
       function initTheDeviceAcitons()
        {
          PreventAAction("devices");
          $(".editDevice","#devices").click(function(e)
            {
              var id = $(this).attr("data-id");
              var url="@Url.Action("Edit", "AdminDevice")"+"?deviceID="+id;
              $("#deviceEditDialog").load(url);
              $("#deviceEditDialog").dialog( "open" );
              e.preventDefault();
            });
            $("#createDevice","#devices").click(function(e)
            {
              var url="@Url.Action("Add", "AdminDevice")";
              $("#deviceAddDialog").load(url);
              $("#deviceAddDialog").dialog( "open" );
              e.preventDefault();
            });
            $(".deleteDevice","#devices").click(function(e)
            {
             var isContinue=confirm("你确定要删除这个设备");
             if(isContinue)
             {
              $.post("@Url.Action("Delete", "AdminDevice")",{id:$(this).attr("data-id")},function()
              {
               $("#devices").load($("#devices").attr("data-url"),function()
               {
               initTheDevice();
               });
               alert("删除成功");
              });
             }
              e.preventDefault();
              });
         }

      function initTheDevice(){
          var bsID=$("#bsID","#bsEditForm").val();
         var identify=$("#searchIdentify").val();
         var type=$("#searchType").val();
         var url="@Url.Action("List", "AdminDevice")";
         url=encodeURI(url+"?bsID="+bsID+"&identify="+identify+"&type="+type);
         $("#devices").attr("data-url",url);       
         $("#devices").load(url,function(){
            initTheDeviceAcitons();
         });
      }
      function initTheNOBSIDDevice(){
            PreventAAction("deviceNOBSIDTable");
            $(".submitNOBSIDDevice","#deviceNOBSIDTable").click(function(e)
            {
             var isContinue=confirm("你确定将此设备相关到本业务系统");
             if(isContinue)
             {
                 $.post("@Url.Action("SubmitNOBSIDDevice", "AdminDevice")",
                  "&SiteId="+$("#bsSiteID","#bsEditForm").val()+"&BusinessSystemId="+$("#bsID","#bsEditForm").val()+"&deviceID="+$(this).attr("data-id")+"&identify="+$(this).attr("data-identify"),
                        function () {
                        $("#devices").load($("#devices").attr("data-url"),function()
                        {
                           initTheDevice();
                        });
                        $("#devicesNOBSID").load($("#devicesNOBSID").attr("data-url"),function()
                        {
                           initTheNOBSIDDevice();
                        });
                        });
             }
              e.preventDefault();
            });
            }
        
        function treeView(){
             $("#bsTree").treeview({
                control: "#bsTreeControl",
                persist: "cookie",
                cookieId: "treeview-black_BusinessSystemTreeMaintaince",
                collapsed: true,
                unique: true
              });
             
            $(".node").click(function (e) {
                var id = $(this).attr("data-id");
                var url="@Url.Action("Edit", "AdminBS")"+"?bsID="+id;
                $("#content").load(url,function()
                {
                initTheEditor();
                });
                var url2=encodeURI("@Url.Action("List", "AdminDevice")"+"?bsID="+id);
                $("#devices").load(url2,function()
                {
                initTheDevice();
                });
                $("#devices").attr("data-url",url2);
                var url3="@Url.Action("NoBSIDList", "AdminDevice")"+"?bsID="+id;
                $("#devicesNOBSID").load(url3,function()
                {
                initTheNOBSIDDevice();
                });
                $("#devicesNOBSID").attr("data-url",url3);
               
                $("#systemID").val(id);
                e.preventDefault();
            });
                  $("#content").empty();
                  $("#devices").empty();
                  $("#devicesNOBSID").empty();
        }


        $(document).ready(function () {
            treeView();
            $("#searchDevice").click(function(){
            initTheDevice();
            });
            $("#searchClearDevice").click(function(){
            $("#searchIdentify").val("");
            $("#searchType")[0].selectedIndex=0;
            initTheDevice();
            });
            $("#deviceAddDialog").dialog({
            autoOpen: false, width: 350, height: 260, modal: true,
            buttons: {
                "保存": function () {
                if($("#Device_Identify","#deviceAddForm").val()=="")
                {
                alert("标识符不能为空！")
                }
                else{
                    $.post("@Url.Action("Add", "AdminDevice")",
                        $("#deviceAddForm").serialize()+"&Device.SiteId="+$("#bsSiteID","#bsEditForm").val()+"&Device.BusinessSystemId="+$("#bsID","#bsEditForm").val(),
                        function () {
                            $("#deviceAddDialog").dialog("close");
                             $("#devices").load($("#devices").attr("data-url"),function()
                {
                initTheDevice();
                });
                        });
                }
                },
                "取消": function () { $(this).dialog("close"); }
            }
            });
            $("#deviceEditDialog").dialog({
            autoOpen: false, width: 350, height: 260, modal: true,
            buttons: {
                "保存": function () {
                    $.post("@Url.Action("Edit", "AdminDevice")",
                        $("#deviceEditForm").serialize(),
                        function () {
                            $("#deviceEditDialog").dialog("close");
                            $("#devices").load($("#devices").attr("data-url"),function()
                {
                initTheDevice();
                });
                        });
                },
                "取消": function () { $(this).dialog("close"); }
            }
            });
        });
    </script>
}
